let upload = require('../../common/upload')
Page({
    behaviors: [upload],
    data: {
        message: '',
        voucher: [],
        voucherFormData: { from: 'comment' },
        buttonLoading: false
    },
    submit() {
        if (this.data.message === '') return wx.showToast({
            title: '请输入反馈内容',
            icon: 'none'
        })
        if (this.data.message.length < 10) return wx.showToast({
            title: '反馈内容不得少于10个字',
            icon: 'none'
        })
        for (let i = 0; i < this.data.voucher.length; i++) {
            if (this.data.voucher[i].status !== 'done') {
                return wx.showToast({
                    title: '有图片正在上传或上传失败',
                    icon: 'none'
                })
            }
        }
        this.setData({ buttonLoading: true })
        const { message, voucher } = this.data

        let reqData = {
            title: '蚂蚁花团意见反馈',
            content: message
        }

        if (voucher.length > 0) {
            let photos = voucher.map(item => {
                return item.uploadPath
            })
            reqData.photos = JSON.stringify(photos)
        }
        this.request({
            url: '/api/user/feedback',
            method: 'POST',
            data: reqData
        }, () => {
            wx.$dialog.alert({
                title: '提示',
                message: '提交成功，感谢您的反馈！',
                theme: 'round-button'
            }).then(() => {
                wx.navigateBack()
            })
        }, () => {
            this.setData({
                buttonLoading: false
            })
        })
    }
})